package com.btpj.module_main.ui.project

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.btpj.jetpack.base.BaseViewModel
import com.btpj.jetpack.utils.ToastUtil
import com.btpj.jetpack.entity.ApiResult
import com.btpj.module_main.data.entity.ProjectTabItem
import com.btpj.module_main.data.remote.repository.MainRepository
import kotlinx.coroutines.launch

class ProjectViewModel(private val repository: MainRepository) : BaseViewModel() {

    val tabDataLiveData = MutableLiveData<MutableList<ProjectTabItem>>()

    private fun getTabData() {
        viewModelScope.launch {
            when (val tabData = repository.getTabData()) {
                is ApiResult.Success -> tabDataLiveData.postValue(tabData.data)
                is ApiResult.Error -> ToastUtil.showShort(tabData.exception.errorMsg ?: "请求失败")
            }
        }
    }

    override fun start() {
        getTabData()
    }
}